using DG.Tweening;
using UnityEngine;

public class TextMeshAnim : MeshAnimBase
{
    private Transform[] _transforms;
    private Vector3[] _scales;
    
    protected override void Init()
    {
        var textMeshes = gameObject.GetComponentsInChildren<TextMesh>();
        _count = textMeshes.Length;
        _transforms = new Transform[_count];
        _scales = new Vector3[_count];
        for (var i = 0; i < _count; i++)
        {
            _transforms[i] = textMeshes[i].transform;
            _scales[i] = _transforms[i].localScale;
        }
    }

    protected override void SetBeginState()
    {
        for (int i = 0; i < _count; i++)
        {
            _transforms[i].localScale = Vector3.zero;
        }
    }

    protected override void PlayForwardAnimation()
    {
        for (var i = 0; i < _count; i++)
        {
            var duration = Random.Range(m_minDuration, m_maxDuration);
            var delay = Random.Range(m_minDelay, m_maxDelay);
            _transforms[i].DOScale(_scales[i], duration).SetDelay(delay);
        }
    }

    protected override void PlayBackwardAnimation()
    {
        for (var i = 0; i < _count; i++)
        {
            var duration = Random.Range(m_minDuration, m_maxDuration);
            var delay = Random.Range(m_minDelay, m_maxDelay);
            _transforms[i].DOScale(Vector3.zero, duration).SetDelay(delay);
        }
    }
}
